package com.hualan.controller;

import com.hualan.pojo.Result;
import com.hualan.utils.FileUploadUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;


@RestController
public class UploadController {
    private  final  FileUploadUtil fileUploadUtil;

    public UploadController(FileUploadUtil fileUploadUtil) {
        this.fileUploadUtil = fileUploadUtil;
    }
    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file) throws IOException {
        try {
            String fileUrl = fileUploadUtil.upload(file);
            return Result.success(fileUrl);
        } catch (IllegalArgumentException e) {
            return Result.error(e.getMessage());
        } catch (IOException e) {
            return Result.error("上传失败: " + e.getMessage());
        }
    }
}
